package com.kykj.kykjyzw.controller;

import com.kykj.kykjyzw.entity.ResultEntity;
import com.kykj.kykjyzw.entity.UserRouteEntity;
import com.kykj.kykjyzw.service.impl.RouteServiceImpl;
import com.kykj.kykjyzw.vo.RouteVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("v2/route/")
@RestController
@CrossOrigin //解决跨域问题
public class RouteController {
    @Resource
    RouteServiceImpl routeService;

    /**
     * 获取路由id
     * @param menuid
     * @return
     */
    @GetMapping("route")
    private ResultEntity getRouteByMenuId(@RequestParam("menuid") int menuid){
        ResultEntity resultEntity = new ResultEntity();
        try{
            UserRouteEntity userRouteEntity = routeService.getRouteByMenuId(menuid);
            if (userRouteEntity==null){
                resultEntity.setCode(1);
                resultEntity.setMsg("查无此id");
            }else {
                resultEntity.setCode(0);
                resultEntity.setData(userRouteEntity);
                resultEntity.setMsg("查询成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-9999);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }
    /**
     * 获取路由列表
     * @param name 路由名称
     * @param controller 路径
     * @param page 页码
     * @param limit 每页行数
     * @return List
     */
    @GetMapping("getroutelist")
    private ResultEntity getRouteList(@RequestParam("name") String name,
                                      @RequestParam("controller") String controller,
                                      @RequestParam("page") int page,
                                      @RequestParam("limit") int limit
    ){
        ResultEntity resultEntity = new ResultEntity();
        try{
            int start = (page-1)*limit; //计算起始位置
            List<UserRouteEntity> list = routeService.getRouteList(name,controller,start,limit); //获取列表
            if(name.equals("") && controller.equals("")){ //如果没有查询条件
                resultEntity.setCode(routeService.getRouteCount()); //获取所有的数据
            }else {
                resultEntity.setCount(list.size()); //获取当前列表所有的数据
            }
            resultEntity.setData(list);
            resultEntity.setCode(0);
        }catch (Exception exception){
            exception.printStackTrace();
            resultEntity.setCode(-9999);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }
    /**
     * 添加路由
     * @param routeVo 路由对象
     * @return ResultEntity
     */
    @PostMapping("route")
    private ResultEntity addRoute(@RequestBody RouteVo routeVo){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if (routeService.addRoute(routeVo)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("添加成功！");
            }else {
                resultEntity.setCode(0);
                resultEntity.setMsg("添加失败！");
            }
        }catch (Exception exception){
            exception.printStackTrace();
            resultEntity.setCode(-9999);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }
    /**
     * 修改路由
     * @param routeVo 路由对象
     * @return ResultEntity
     */
    @PutMapping("route")
    private ResultEntity updateRpute(@RequestBody RouteVo routeVo){
        ResultEntity resultEntity=new ResultEntity();
        try {
            if (routeService.updateRoute(routeVo)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("修改成功！");
            }else {
                resultEntity.setCode(0);
                resultEntity.setMsg("修改失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误！");
        }
        return resultEntity;
    }
    /**
     * 删除路由
     * @param menuid 路由ID
     * @return ResultEntity
     */
    @DeleteMapping("route/{menuid}")
    private ResultEntity delRpute(@PathVariable("menuid") int menuid){
        ResultEntity resultEntity=new ResultEntity();
        try {
            if (routeService.delRoute(menuid)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("删除成功！");
            }else {
                resultEntity.setCode(0);
                resultEntity.setMsg("删除失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-9999);
            resultEntity.setMsg("系统错误！");
        }
        return resultEntity;
    }

}
